package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecGroupService groupService;
    @Autowired
    private SpecParamService paramService;


    /**
     * 根据商品分类id，查询规格组的集合
     * @param id        商品分类id
     * @return          规格组的集合
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategoryId(@RequestParam("id")Long id) {
        //查询
        List<SpecGroup> specGroupList = groupService.query().eq("category_id", id).list();
        //返回
        return ResponseEntity.ok(SpecGroupDTO.convertEntityList(specGroupList));
    }

    /**
     * 根据条件查询规格参数集合
     * @param categoryId        商品分类id
     * @param groupId           规格组id
     * @param searching         是否搜索
     * @return                  规格参数集合
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParamsByCondition(
            @RequestParam(value = "categoryId",required = false)Long categoryId,
            @RequestParam(value = "groupId",required = false)Long groupId,
            @RequestParam(value = "searching",required = false)Boolean searching) {
        //查询
        List<SpecParamDTO> list = paramService.querySpecParamsByCondition(categoryId,groupId,searching);
        //返回
        return ResponseEntity.ok(list);
    }

    /**
     * 新增规格租
     * @param specGroupDTO      SpecGroupDTO对象
     * @return                  新增规格租，无返回值
     */
    @PostMapping("/group")
    public ResponseEntity<Void> saveSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        //添加
        groupService.save(specGroupDTO.toEntity(SpecGroup.class));
        //返回
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格组
     * @param specGroupDTO         SpecGroupDTO对象
     * @return                      wu
     */
    @PutMapping("/group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        //修改
        groupService.updateById(specGroupDTO.toEntity(SpecGroup.class));
        //返回
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     * @param specParamDTO      规格参数对象
     * @return                  无
     */
    @PostMapping("/param")
    public ResponseEntity<Void> saveSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        //添加
        paramService.save(specParamDTO.toEntity(SpecParam.class));
        //返回
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数
     * @param specParamDTO   规格参数对象
     * @return                  无
     */
    @PutMapping("/param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParamDTO specParamDTO) {
        //更新
        paramService.updateById(specParamDTO.toEntity(SpecParam.class));
        //返回
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类id查询规格组及组内参数
     * @param id    商品分类id category_id
     * @return      规格组及组内参数
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupParams(@RequestParam("id")Long id) {
        //查询
        List<SpecGroupDTO> list = groupService.queryGroupParams(id);
        //返回
        return ResponseEntity.ok(list);
    }
}
